package com.suning.sawp.service.impl.dao;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.suning.sawp.intf.dao.CouponRecordDaoService;
import com.suning.sawp.po.promotion.CouponRecord;

@Service("couponRecordDaoService")
public class CouponRecordDaoServiceImpl implements CouponRecordDaoService {

    @Resource
    MongoOperations mongoTemplate;
    
    @Override
    public int countCouponRecord(Date startTime, Date endTime) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and(CouponRecord.Attributes.SEND_TIME).gt(startTime).lt(endTime);
        query.addCriteria(criteria);
        
        int totalCount = (int) mongoTemplate.count(query, CouponRecord.class);
        return totalCount;
    }

    @Override
    public List<CouponRecord> queryCouponRecords(Date startTime, Date endTime, int startIndex, int limit) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and(CouponRecord.Attributes.SEND_TIME).gt(startTime).lt(endTime);
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, CouponRecord.Attributes.SEND_TIME)));
        query.skip(startIndex).limit(limit);
        
        return mongoTemplate.find(query, CouponRecord.class);
    }

}
